jQuery(function(){
  var $ic = com.lolita.get.ic();
  jQuery(document).on('loli.team_admin.close',$ic,function(event){
    com.lolita.utils.remWidgetStyleSheet('team_admin');
  }).on('loli.team_admin.ajaxLoaded',$ic,function(event){
    com.lolita.display.setLoliDialogTitle();
    
    jQuery('.logs',$ic).insertBefore('.ui-dialog-buttonset').show();
    jQuery('.view-me').button({
      "icons" : {"primary" : "ui-icon-person"}
      ,"text" : false
    }).click(function(event){
      var id= this.href.split('#')[1];
      com.lolita.dialog.team(id);
    });

    var team_admin_dynForm = 'team_admin_dynForm';
    var options = {
      'onEmpty' : 'SELF'
    };
    
    com.lolita.display.makeTeamPick('.ac-jouteur-fav-t');
    com.lolita.display.addDynForm('',{
      'callback' : {
        'success': function(){
          jQuery(document).trigger(team_admin_dynForm);
        }
        ,'beforeSubmit' : function(fields,$form,options){
          var ret=true;
          
          if($form.is('.membre-info')){
            var $pwd = $form.find(':password');
            $pwd.removeClass('error');
            if($pwd.length!=2 || $form.find(':password:eq(0)').val()!=$form.find(':password:eq(1)').val()){
              $form.find('.ui-accordion').accordion( "option", "active", 1 );
              $pwd.addClass('error');
              $pwd.effect('pulsate');
              ret=false;
            }
          }
          
          return ret;
        }
      }
    });
    
    jQuery(document).on('change','.widget.team_admin .admin-delete',function(){
      var $this = jQuery(this);
      var $li = $this.closest('li');
      if($this.is(':checked')){
        $li.switchClass('void','to-delete',500);
      } else {
        $li.switchClass('to-delete','void',500);
      }
    });
    jQuery(document).on(team_admin_dynForm,function(){

      com.lolita.redoJsonBrOnTextarea();
      com.lolita.makeColorPicker('.color-picker',{
        onInit : function(pars){
          var $this = jQuery(this);
          $this.css('background-color',$this.val());
        }
      });
      
      com.lolita.display.makeMemberPick('input[id="coach[id]"]');
      com.lolita.display.makeMemberPick('input[id="jouteur[id]"]');
      jQuery('.widget.team_admin').height(parseInt($ic.height()));
      //jQuery('.widget.team_admin form.equipe-jouteurs').height(parseInt($ic.height()));
      com.lolita.display.makeLoliTab('.widget.team_admin .loli-tab');          
      com.lolita.display.makeAccordion('.widget.team_admin .loli-accordion');
      jQuery('.btn-actions').buttonset().find('.admin-delete').button({
         'icons' : { 'primary' : 'ui-icon-trash' }
        ,'text' : false
      }).end().find('.admin-resp').button({
         'icons' : { 'primary' : 'ui-icon-person' }
        ,'text' : false
      });
      
      com.lolita.display.makeTeamPickWithYear('.equipe-year',null,{
        callback : {
          onSelect: function(rep,ui){
            if(rep && rep.item && rep.item.value){
              var $parent = jQuery(this).closest('fieldset');
              $parent.find('.equipe').val(rep.item.value);
              $parent.find('.saison').val(rep.item.saison_id);
            }
          }
        }
      });
    }).trigger(team_admin_dynForm);
  });
  
  
});